package org.zoffy.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authorization.AuthorizationDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.zoffy.commons.BaseResponse;
import org.zoffy.commons.ResultUtils;
import org.zoffy.commons.StatusCode;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(AuthorizationDeniedException.class)
    public BaseResponse<?> authorizationDeniedExceptionHandler(Exception exception){
        return ResultUtils.error(StatusCode.NOT_LOGIN_ERROR);
    }
    @ExceptionHandler(BusinessException.class)
    public BaseResponse<?> businessExceptionHandler(BusinessException businessException){
        return ResultUtils.error(businessException.getCode(),businessException.getMessage());
    }
    @ExceptionHandler(Exception.class)
    public BaseResponse<?> exceptionHandler(Exception exception){
        log.error("Exception: {}",exception.toString(),exception);
        return ResultUtils.error(StatusCode.SYSTEM_ERROR);
    }
}
